package com.kitking.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.kitking.myapplication.lister.HttpCallbackLister;
import com.kitking.myapplication.utils.HttpUtil;
import com.kitking.myapplication.utils.okHttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bun1:
                //HttpUrlConnection里面有线程，所以必须用回调不然数据来不及返回就会请求完成
                //回调接口要自己写
                HttpUtil.sendHttpRequest2("https://www.baidu.com/", new HttpCallbackLister() {
                    @Override
                    public void onFinsh(String response) {
                        String responseData = response;
                        Log.d("测试HTTPURLConnect的封装-----", "OOOOOOO" + responseData);

                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
                break;
            case R.id.bun2:
                //为什么说okhttp方便，是因为不用在写回调函数了吗。是的。而且请求网络的线程
                // 也不用我们自己写了，因为它也帮我们封装好了。就在enqueue方法内部
                okHttpUtil.sendOkHttpRequest("https://www.baidu.com/", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String responseData = response.body().string();
                        Log.d("测试OKHTTP的封装-----", "11111111" + responseData);

                    }
                });
                break;
            case R.id.bun3:
                sendRequest();
                break;
            default:
                Log.d("--TAG--","--------失败了没有一个按钮触发");

        }

    }

    private void sendRequest() {
        //不封装线程的方法，okhttp的new callback回调接口已经写好了，
        // 但是newCall.execute并没有封装线程，所以请求网络时还是要在new Thred的run里跑
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //   Log.d("测试-----","22222222222");

                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("https://www.baidu.com/")
                            .build();
                    Response response = okHttpClient.newCall(request).execute();
                    String responsedata = response.body().string();
                     Log.d("测试okhttp没有封装线程的方法-----","2222222"+responsedata);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
